package com.myrslok.module;

import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Module settings that are backed by the Maven pom.xml.
 */
public class MavenModuleSettings implements ModuleSettings {

    private static final Logger logger = LoggerFactory.getLogger(MavenModuleSettings.class);

    private String moduleName;
    private final File moduleRoot;

    public MavenModuleSettings(String moduleName) {
        this.moduleName = moduleName;
        this.moduleRoot = new File(moduleName);
    }


    @Override
    @NotNull
    public File getModuleRoot() {
        return moduleRoot;
    }

    @Override
    @NotNull
    public Set<File> getSrcDirs() {
        return null;
    }

    @Override
    @NotNull
    public Set<File> getTestDirs() {
        return null;
    }

    @Override
    @NotNull
    public File getBuildDir() {
        return null;
    }

    @Override
    @NotNull
    public File getTargetDir() {
        return moduleRoot;
    }

    @Override
    @NotNull
    public File getTargetProdClassesDir() {
        return null;
    }

    @Override
    @NotNull
    public File getTargetTestClassesDir() {
        return null;
    }

    @Override
    @NotNull
    public File getTargetBuildClassesDir() {
        return null;
    }

    @NotNull
    @Override
    public Set<File> getProductionLibs() {
        return new HashSet<File>();
    }

    @NotNull
    @Override
    public Set<File> getTestLibs() {
        return new HashSet<File>();
    }

    private void parseXml(final ContentHandler contentHandler, final InputStream inputStream) {
        final SAXParserFactory spf = SAXParserFactory.newInstance();
        try {
            final SAXParser sp = spf.newSAXParser();
            final XMLReader xr = sp.getXMLReader();

            xr.setContentHandler(contentHandler);
            xr.parse(new InputSource(inputStream));

        } catch (ParserConfigurationException e) {
            logger.error("Parse " + contentHandler.getClass().getSimpleName() + " File", "XML Passing Exception = ", e);
        } catch (IOException e) {
            logger.error("Parse " + contentHandler.getClass().getSimpleName() + " File", "XML Passing Exception = ", e);
        } catch (SAXException e) {
            logger.error("Parse " + contentHandler.getClass().getSimpleName() + " File", "XML Passing Exception = ", e);
        }
    }

    private ProjectSetup parsePomFile() {
        final ProjectSetup projectSetup = new ProjectSetup();
        parseFile(new File(moduleRoot, "pom.xml"), projectSetup);
        return projectSetup;
    }

    private void parseFile(File file, ContentHandler contentHandler) {
        InputStream inputStream = null;
        try {
            inputStream = new BufferedInputStream(new FileInputStream(file));
            parseXml(contentHandler, inputStream);
        }
        catch (FileNotFoundException e) {
            logger.error("File " + file + " not found", e);
        }
        finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    logger.error("XML Passing Exception (close)", e);
                }
            }
        }
    }


    private class ProjectSetup extends DefaultHandler {
        @NotNull
        private final Set<File> prodDirs = new HashSet<File>();
        @NotNull
        private final Set<File> testDirs = new HashSet<File>();
        private Boolean inheritCompilerOutput;
        private File targetProduction;
        private File targetTest;

        @NotNull
        public Set<File> getProdDirs() {
            return prodDirs;
        }

        @NotNull
        public Set<File> getTestDirs() {
            return testDirs;
        }

        public File getTargetProduction() {
            return targetProduction;
        }

        public File getTargetTest() {
            return targetTest;
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
            // Alex do some pom.xml magic here :-)
        }
    }

}
